package com.jackrain.nea.ad.mapper;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.entity.GroupFetchModel;
import com.jackrain.nea.ad.entity.MenuEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;

/**
 * @author lzm
 * @date 2017-11-15
 */
@Mapper
public interface CmenuMapper {
    @Select("SELECT id,name,IFNULL(description, name) as description FROM GROUPS WHERE ISACTIVE='Y';")
    List<GroupFetchModel> fetchAllGroup();

    @InsertProvider(type = SqlBuilder.class, method = "insertMenu")
    int insertMenu(JSONObject mainRow);

    @UpdateProvider(type = SqlBuilder.class, method = "updateMenu")
    int updateMenu(JSONObject mainRow);

    @Select("SELECT * FROM AD_MENU WHERE ID=#{id} limit 1")
    @Results({
            @Result(property = "ad_client_id", column = "ad_client_id"),
            @Result(property = "ad_org_id", column = "ad_org_id"),
            @Result(property = "ad_subsystem_id", column = "ad_subsystem_id"),
            @Result(property = "ad_tablecategory_id", column = "ad_tablecategory_id"),
            @Result(property = "ad_table_id", column = "ad_table_id"),
            @Result(property = "ad_action_id", column = "ad_action_id")
    })
    MenuEntity getMenuById(Long id);

    @Select("SELECT count(1) FROM AD_MENU WHERE AD_TABLE_ID =#{tableId} AND ID <>#{selfId} AND TYPE =1")
    int countMenuByTable(@Param(value = "tableId")Long tableId, @Param(value = "selfId")Long selfId);

    @Select("SELECT count(1) FROM AD_MENU WHERE AD_ACTION_ID =#{actionId} AND ID <>#{selfId} AND TYPE =2")
    int countMenuByAction(@Param(value = "actionId")Long actionId, @Param(value = "selfId")Long selfId);

    @Select("select count(1) from AD_MENU where id=#{id}")
    int countMenuById(long id);

    @Delete("delete from AD_MENU where id=#{id}")
    int delMenu(long id);

    @Select("SELECT menu.* FROM AD_MENU menu, ad_subsystem sub, ad_tablecategory cate\n" +
            "WHERE menu.AD_SUBSYSTEM_ID = sub.id and menu.AD_TABLECATEGORY_ID=cate.id and  menu.ISACTIVE='Y' ORDER BY sub.orderno, sub.id, cate.orderno, cate.id, orderno, id")
    @Results({
            @Result(property = "ad_client_id", column = "ad_client_id"),
            @Result(property = "ad_org_id", column = "ad_org_id"),
            @Result(property = "ad_subsystem_id", column = "ad_subsystem_id"),
            @Result(property = "ad_tablecategory_id", column = "ad_tablecategory_id"),
            @Result(property = "ad_table_id", column = "ad_table_id"),
            @Result(property = "ad_action_id", column = "ad_action_id")
    })
    List<MenuEntity> fetchAllMenus();

    //排除掉tabitem（明细菜单）的动作定义，这部分的动作定义是不需要控制权限的，只要显示主表就会相应的显示明细。
    @Select("select ID, AD_TABLE_ID, AD_ACTION_ID from ad_action WHERE ISACTIVE='Y' and DISPLAYTYPE<>'tabitem' order by ad_table_id, ad_action_id, priority")
    List<HashMap<String, Long>> fetchAllActions();

    class SqlBuilder{
        public String insertMenu(JSONObject map){
            return new SQL(){
                {
                    INSERT_INTO("AD_MENU");
                    for (String key : map.keySet()) {
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }

        public String updateMenu(JSONObject map){
            return new SQL(){
                {
                    UPDATE("AD_MENU");
                    for(String key : map.keySet()){
                        if(!"ID".equals(key)){
                            SET(key + "= #{" + key + "}");
                        }
                    }
                    WHERE("ID = #{ID}");
                }
            }.toString();
        }
    }
}
